package com.hygj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hygj.pojo.Role;
import com.hygj.pojo.User;
import com.hygj.service.RoleService;
import com.hygj.util.PageUtil;

@Controller
@RequestMapping
public class RoleController {

	@Autowired
	private RoleService roleService;

	static Map<String, String> roles = new HashMap<String, String>();

	/**
	 * 增加角色
	 * 
	 * @param login
	 * @return
	 */
	@RequestMapping("roleInsert")
	public String roleInsert(Role role, HttpServletRequest request) {
		roleService.insert(role);
		return "redirect:/getRoleAll";
	}

	/**
	 * 查询全部角色(列表用)
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getRoleAll")
	public String getLoginAll(Map<String, Object> map, HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		PageUtil<Role> page = roleService.page(request, 10, params);
		map.put("page", page);
		return "logins/RoleShow";
	}

	/**
	 * 查询角色---添加用户使用
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getRole")
	public String getRoleAll(Map<String, Object> map, HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<Role> page = roleService.listAll("listAll", params);
		// list转换成Map
		Map<String, String> _r = new HashMap<String, String>();
		for (Role role : page) {
			_r.put("" + role.getRid(), role.getRole_name());
		}
		map.put("roles", _r);
		map.put("user", new User());
		return "logins/UserInsert";
	}

	/**
	 * 回显角色信息(根据‘rid’)
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getRoleById")
	public String getRoleById(@RequestParam("rid") Integer rid, ModelMap modelMap) {
		Role roles = roleService.getById("getById", rid);
		modelMap.addAttribute("roles", roles);
		return "logins/RoleUpdate";
	}

	/**
	 * 角色信息修改
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("updateRole")
	public String updateRole(Role role) {
		roleService.update("update", role);
		return "redirect:getRoleAll";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteRole")
	public String deleteRole(@RequestParam("rid") Integer rid) {
		roleService.delete("delete", rid);
		return "redirect:getRoleAll";
	}

}
